<template>
  <div class="container">
    <el-dialog
      title="单品多店添加订单"
      :visible.sync="addDialog.centerDialogVisible"
      @open="openData"
      @close="cancel"
      center
      width="99%"
    >
      <div class="top">
        <el-switch
          v-model="is_hangCai"
          active-text="行采"
          inactive-text="非行采"
          @change="hangCai()"
        >
        </el-switch>
      </div>
      <div class="top" v-if="!is_hangCai">
        <el-switch
          v-model="is_assign"
          @change="assign()"
          active-text="批次出(成本价)"
          inactive-text="先入先出(平均价)"
        >
        </el-switch>
      </div>
      <!-- <div class="top">
        <el-select
          style="margin-bottom: 5px"
          v-model="organization"
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          placeholder="请选择机构"
          :remote-method="organizationMethod"
          :loading="insides.loading"
        >
          <el-option
            v-for="item in insides.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div> -->

      <div class="top">
        <el-select
          size="small"
          v-model="search.supplier_code"
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          placeholder="请选择供应商"
          :remote-method="supplierRemoteMethod"
          @change="supplierOn"
          :loading="supplierCenter.loading"
        >
          <el-option
            v-for="item in supplierCenter.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="top" v-if="is_hangCai">
        <el-select
          size="small"
          v-model="search.buyer_code"
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          placeholder="请选择采购员"
          :remote-method="insidePurchaseMethod"
          :loading="inside.loading"
          @change="getPurchases(search.buyer_code)"
        >
          <el-option
            v-for="item in purchase.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="top">
        <el-select
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          v-model="selectList"
          placeholder="请搜索选择配送中心"
          :remote-method="selectListsRemoteMethod"
          :loading="selectLists.loading"
          @change="distribution(selectList)"
        >
          <el-option
            v-for="item in selectLists.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>

      <div class="top">
        <el-select
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          v-model="commodity"
          placeholder="请搜索选择商品"
          :remote-method="insideRemoteMethod"
          :loading="inside.loading"
        >
          <el-option
            v-for="item in inside.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          size="medium"
          type="primary"
          class="handleSearch"
          icon="el-icon-search"
          @click="addGoods()"
          >添加</el-button
        >
      </div>
      <el-table
        ref="isUpdate"
        :data="tableData"
        border
        max-height="500"
        style="width: 100%"
        empty-text="暂无数据"
        :key="Math.random()"
      >
        <el-table-column
          prop="goods_name"
          label="商品名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="erp_goods_code"
          label="商品编码"
          align="center"
        ></el-table-column>
        <!-- <el-table-column
          prop="goods_spec"
          label="规格"
          align="center"
        ></el-table-column> -->
        <el-table-column
          prop="category_code"
          label="分类编码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="category_name"
          label="分类名称"
          align="center"
        ></el-table-column>
        <!-- <el-table-column
          prop="goods_unit"
          label="单位"
          align="center"
        ></el-table-column> -->

        <el-table-column
          prop="bar_code"
          label="条形码"
          align="center"
        ></el-table-column>
        <!-- <el-table-column
          v-if="!is_assign"
          prop="avg_price"
          label="平均价"
          align="center"
        ></el-table-column>
        <el-table-column
          v-else
          prop="cost_price"
          label="成本价"
          align="center"
        ></el-table-column> -->
        <!-- <el-table-column
          v-if="!is_assign"
          prop="stockRemoval"
          label="出库价"
          align="center"
        ></el-table-column>
        <el-table-column
          v-else
          prop="stockRemovals"
          label="出库价"
          align="center"
        ></el-table-column> -->

        <el-table-column
          v-if="is_assign"
          label="批次商品"
          align="center"
          width="150px"
        >
          <template v-if="is_assign" slot-scope="scope">
            <el-select
              v-model="scope.row.batch_id"
              placeholder="请选择批次商品"
              @change="onBatch(scope)"
            >
              <el-option
                v-for="item in scope.row.unAvgBatchList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :title="item.code"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          v-if="!is_hangCai"
          prop="rk_specs"
          label="入库箱规"
          align="center"
        ></el-table-column>
        <el-table-column
          v-if="is_hangCai"
          prop="SonCounts"
          width="150px"
          label="入库箱规"
          align="center"
        >
          <template v-if="is_hangCai" slot-scope="scope">
            <el-select
              v-model="scope.row.rk_spec"
              @change="sonCount(scope)"
              placeholder="请选择"
            >
              <el-option
                v-for="item in scope.row.SonCountsList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="生产日期" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.produce_date }}</p>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="到期日" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.due_date }}</p>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="保质期（天）" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.shelf_life }}</p>
          </template>
        </el-table-column>
        <el-table-column
          v-if="is_hangCai"
          label="生产日期"
          align="center"
          width="250px"
        >
          <template v-if="is_hangCai" slot-scope="scope">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="scope.row.produce_date"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column
          v-if="is_hangCai"
          label="到期日"
          align="center"
          width="250px"
        >
          <template slot-scope="scope">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="scope.row.due_date"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column
          v-if="is_hangCai"
          label="保质期（天）"
          align="center"
          width="150px"
        >
          <template v-if="is_hangCai" slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.shelf_life"
            ></el-input-number>
          </template>
        </el-table-column>
        <!-- <el-table-column label="数量" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              @change="orderNum(scope)"
              :controls="false"
              v-model="scope.row.order_num"
            ></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="加价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              @change="markup(scope)"
              :controls="false"
              v-model="scope.row.markup"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
          v-if="is_hangCai"
          label="成本价"
          align="center"
          width="150px"
        >
          <template slot-scope="scope">
            <el-input-number
              size="small"
              @change="markup(scope)"
              :controls="false"
              v-model="scope.row.cost_price"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column v-else label="件数" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :controls="false"
              v-model="scope.row.box_qty"
            ></el-input-number>
          </template>
        </el-table-column>  -->

        <el-table-column label="机构配货信息" align="center" width="950px">
          <template slot-scope="scope">
            <el-select
              style="margin-bottom: 5px; width: 500px"
              v-model="scope.row.orgCode"
              filterable
              multiple
              remote
              center
              reserve-keyword
              placeholder="请选择机构"
              :remote-method="organizationMethod"
              :loading="insides.loading"
            >
              <el-option
                v-for="item in insides.option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
            <el-button
              size="medium"
              type="primary"
              class="handleSearch"
              icon="el-icon-search"
              @click="addOrganization(scope)"
              >添加</el-button
            >
            <el-upload
              v-if="is_assign"
              class="upload-demo"
              style="display: inline; margin-left: 10px"
              action="#"
              :show-file-list="false"
              :http-request="handleAvatarSuccess"
            >
              <el-button
                size="medium"
                type="primary"
                class="handleSearch"
                icon="el-icon-circle-plus"
                >模版导入</el-button
              >
            </el-upload>
            <el-button
              v-if="is_assign"
              size="medium"
              type="info"
              class="handleSearch"
              icon="el-icon-upload"
              @click="templateDownload()"
              >模版下载</el-button
            >
            <div v-for="(item, index) in scope.row.orgList" :key="item.id">
              <p class="orgTop">
                {{ item.orgName }}
                <el-button
                  class="remBtn"
                  size="small"
                  type="danger"
                  @click="removeOrg(scope, index)"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </p>
              <div>
                <span>
                  订单数量:
                  <el-input-number
                    @change="orderNum(scope, item)"
                    size="small"
                    :controls="false"
                    v-model="item.order_num"
                  ></el-input-number>
                </span>
                <span v-if="!is_hangCai">
                  箱数:
                  <el-input-number
                    @change="qtyup(scope, item)"
                    size="small"
                    :controls="false"
                    v-model="item.box_qty"
                  ></el-input-number>
                </span>
                <span>
                  加价:
                  <el-input-number
                    size="small"
                    :controls="false"
                    v-model="item.markup"
                    @change="markup(item, scope)"
                    :disabled="
                      scope.row.fixed_priceType || scope.row.m_type == 2
                    "
                  ></el-input-number>
                </span>
                <span>
                  成本价:
                  <el-input-number
                    size="small"
                    :controls="false"
                    v-model="item.cost_price"
                    @change="markup(item, scope)"
                    :disabled="!is_hangCai"
                    :max="
                      is_hangCai && item.reference_price > 0
                        ? item.reference_price
                        : Infinity
                    "
                  ></el-input-number>
                </span>
                <span v-if="is_hangCai">
                  参考价:{{ item.reference_price }}
                </span>
                <span> 采购加价:{{ item.purchasePrice }} </span>
                <span style="color: red">
                  出库价:{{ item.stockRemoval }}
                  <!-- <el-input-number
                    size="small"
                    :controls="false"
                    v-model="item.stockRemoval"
                  ></el-input-number> -->
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="is_assign"
          label="门店到货价"
          align="center"
          width="150px"
        >
          <template v-if="is_assign" slot-scope="scope">
            <el-input-number
              size="small"
              @change="orderFixedPprice(scope)"
              :controls="false"
              v-model="scope.row.fixed_price"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
          v-if="!is_assign"
          label="预提"
          align="center"
          width="150px"
        >
          <template v-if="!is_assign" slot-scope="scope">
            <div>{{ scope.row.withholding_fee }}</div>
          </template>
        </el-table-column>
        <div v-else>
          <el-table-column label="预提" align="center">
            <template slot-scope="scope">
              <div>{{ scope.row.withholding_fees }}</div>
            </template>
          </el-table-column>
        </div>
        <el-table-column label="包装箱" align="center" width="150px">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.package_box_id"
              @change="rdcBox(scope)"
              placeholder="请选择"
            >
              <el-option
                :key="item.id"
                v-for="item in boxList"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="包装箱规格" align="center" width="200px">
          <template slot-scope="scope">
            <div>
              <el-input-number
                size="small"
                :precision="4"
                v-if="scope.row.rdcType"
                :controls="false"
                v-model="scope.row.package_box_specInp"
              ></el-input-number>
              <span v-else>{{ scope.row.package_box_specInp }}</span>
              <span
                v-if="scope.row.rdcType"
                style="line-height: 30px;margin-left: 5px;"
                >KG</span
              >
              <span v-else style="line-height: 30px;margin-left: 5px;">个</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="备注" align="center" width="250px">
          <template slot-scope="scope">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="scope.row.notes"
            >
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="danger"
              @click="remove(scope)"
              icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              v-if="is_hangCai"
              size="small"
              type="primary"
              class="handleSearch"
              icon="el-icon-circle-plus"
              @click="addSpec(scope)"
              >添加规格</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        width="400px"
        title="添加规格"
        center
        :visible.sync="spaceDialog"
        append-to-body
        @close="cancelSpec"
      >
        <el-form label-width="100px" label-position="center">
          <el-form-item label="规格：">
            <el-input-number
              size="small"
              :min="1"
              :controls="false"
              v-model="spec"
            ></el-input-number>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelSpec">取 消</el-button>
          <el-button type="primary" @click="specNum()">确 定</el-button>
        </span>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确认发货</el-button>
        <el-button type="primary" @click="addOrder">只生成订单</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addOrderInfo,
  selectErpOrderGoodsData,
  getUnAvgBatch,
  createOrderByMultiStore,
  createOrderByMultiStoreNoSend,
  getPackageBoxData
} from "@/api/superOrderNew/superOrderList";
import { getToken } from "@/utils/auth";
import { Loading } from "element-ui";
import { getOrgData } from "@/api/middleorder/middleorderlist";
import { getOpenOrgData } from "@/api/middleorder/middleorderlist";
import { selectRdcData } from "@/api/select/select";
import { selectSupplierData } from "@/api/supplier/supplierList"; //供应商
import { getList } from "@/api/putStorage/putStotageList"; //采购员
export default {
  props: {
    addDialog: Object
  },
  data() {
    return {
      disabled: true,
      tableData: [],
      unAvgBatchData: [],

      commodity: "",
      is_assign: false,
      is_hangCai: false,
      inside: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      insides: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      isUpdate: 0,
      search: {
        goods_code: "",
        org_code: "",
        supplier_code: "",
        buyer_code: "",
        buyer_name: "",
        buyer_id: ""
      },
      organization: "",
      priceNum: 0,
      selectLists: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      supplierCenter: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      purchase: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      },
      selectList: "",
      distributionCenter: {
        warehouse_code: "",
        rdc_code: ""
      },
      spaceDialog: false,
      spec: "0",
      specId: "",
      boxData: {},
      boxList: [],
      rdc: null
    };
  },
  methods: {
    //取消
    cancel() {
      this.addDialog.centerDialogVisible = false;
      this.inside = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.insides = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.purchase = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.supplierCenter = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.is_assign = false;
      this.is_hangCai = false;
      this.commodity = "";
      this.organization = "";
      this.tableData = [];
      this.search = {
        goods_code: "",
        org_code: "",
        supplier_code: "",
        buyer_code: "",
        buyer_name: "",
        buyer_id: ""
      };
      this.selectLists = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.selectList = "";
      this.distributionCenter = {
        warehouse_code: "",
        rdc_code: ""
      };
    },
    getData() {
      let data = {
        page: 1,
        limit: 500,
        goods_code: this.search.goods_code,
        rdc_code: this.distributionCenter.rdc_code,
        warehouse_code: this.distributionCenter.warehouse_code,
        is_can_use: 3
      };
      //获取商品
      selectErpOrderGoodsData(data).then(res => {
        this.goodsList = res.data.paginate.data;
        if (res.data.paginate.data) {
          this.inside.option = res.data.paginate.data.map(item => {
            return { value: `${item.Code}`, label: `${item.Name}` };
          });
        }
      });
    },
    openData() {
      this.getDatas();
      this.getSupplier();
      this.getPackageBox();
      this.getselsetData();
    },
    getDatas() {
      let data = {
        page: 1,
        limit: 500,
        key: this.search.org_code
      };
      //获取机构
      getOpenOrgData(data).then(res => {
        if (res.data) {
          this.insides.option = res.data.map(item => {
            return {
              value: `${item.erp_org_code}`,
              label: `${item.name}`,
              org_area_code: `${item.org_area_code}`
            };
          });
        }
      });
    },
    getPackageBox() {
      getPackageBoxData().then(res => {
        this.boxData = res.data.package;
        this.rdc = res.data.rdc;
      });
    },
    rdcBox(scope) {
      let id = scope.row.package_box_id;
      this.boxList.forEach((res, index) => {
        if (res.id == scope.row.package_box_id) {
          if (index == 0) {
            scope.row.rdcType = false;
            scope.row.package_box_specInp = 1;
          } else {
            scope.row.rdcType = true;
            scope.row.package_box_specInp = scope.row.package_box_spec;
          }
        }
      });
      console.log(scope.row.package_box_id, 9999);
    },
    getselsetData() {
      let data = {
        page: 1,
        limit: 500,
        code_or_name: this.search.selectnamecode
      };
      //获取配送中心
      selectRdcData(data).then(res => {
        if (res.data.paginate.data) {
          this.selectLists.option = res.data.paginate.data.map(item => {
            return {
              value: `${item.d_code}`,
              label: `${item.d_name}`,
              warehouse_code: `${item.warehouse_code}`,
              org_area_code: `${item.area_code}`,
              rdc_attr: `${item.rdc_attr}`
            };
          });
        }
      });
    },
    getPurchase(key = "") {
      let data = {
        page: 1,
        limit: 100,
        key
      };
      //获取商超采购员列表
      getList(data).then(res => {
        this.purchase.option = res.data.map(item => {
          return {
            value: item.code,
            label: item.name,
            id: item.id
          };
        });
      });
    },
    insidePurchaseMethod(query) {
      if (query !== "") {
        this.getPurchase(query);
      } else {
        this.purchase.option = [];
      }
    },
    getPurchases(e) {
      let data = this.purchase.option.filter(res => {
        return res.value == e;
      });
      this.search.buyer_code = data[0].value;
      this.search.buyer_name = data[0].label;
      this.search.buyer_id = data[0].id;
    },
    getSupplier(name = "") {
      let data = {
        page: 1,
        limit: 100,
        name
      };
      //获取供应商列表
      selectSupplierData(data).then(res => {
        this.supplierCenter.option = res.data.paginate.data.map(item => {
          return {
            value: `${item.supplier_code}`,
            label: `${item.supplier_name}`
          };
        });
      });
    },

    // 嵌套搜索供应商数据
    supplierRemoteMethod(query) {
      if (query !== "") {
        this.getSupplier(query);
      } else {
        this.supplierCenter.option = [];
      }
    },
    // 供应商选择
    supplierOn() {
      if (!this.is_hangCai) {
        this.tableData = [];
      }
    },
    // 嵌套搜索配送方式数据
    selectListsRemoteMethod(query) {
      if (query !== "") {
        this.houseList(query);
      } else {
        this.selectLists.option = [];
      }
    },
    //获取配送数据
    houseList(query) {
      this.search.selectnamecode = query;
      this.getselsetData();
    },
    distribution(code) {
      let arr = this.selectLists.option.filter(res => {
        return res.value == code;
      });
      if (arr[0].rdc_attr == "0") {
        this.boxList = this.boxData.shengnei;
      } else {
        this.boxList = this.boxData.shengwai;
      }
      this.distributionCenter = {
        warehouse_code: arr[0].warehouse_code,
        rdc_code: arr[0].value
      };
      this.tableData = [];
      this.inside = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false
      };
      this.commodity = "";
    },
    //修改加价率和成本价
    markup(item, scope) {
      if (this.is_assign) {
        console.log(121212);
        //批次出
        let purchasePriceCenter = (
          Math.round(
            (parseFloat(item.cost_price) +
              parseFloat(item.markup) +
              parseFloat(scope.row.withholding_fees)) *
              parseFloat(scope.row.BuyerProfitRate) *
              10000
          ) / 10000
        ).toFixed(4);
        item.purchasePrice = scope.row.BuyerProfitMode
          ? scope.row.BuyerProfitRate
          : purchasePriceCenter;
        item.stockRemoval = (
          parseFloat(item.cost_price) +
          parseFloat(item.markup) +
          parseFloat(scope.row.withholding_fees) +
          parseFloat(item.purchasePrice)
        ).toFixed(4);
      } else {
        //先入先出计算
        console.log(item, scope.row, 232323);
        if (scope.row.m_type == 2) {
          item.markup = (
            parseFloat(item.cost_price) *
            (parseFloat(item.markups) / 100)
          ).toFixed(4);
        }
        let purchasePriceCenter = (
          Math.round(
            (parseFloat(item.cost_price) +
              parseFloat(item.markup) +
              parseFloat(scope.row.withholding_fee)) *
              parseFloat(scope.row.BuyerProfitRate) *
              10000
          ) / 10000
        ).toFixed(4);
        item.purchasePrice = scope.row.BuyerProfitMode
          ? scope.row.BuyerProfitRate
          : purchasePriceCenter;
        item.stockRemoval = (
          parseFloat(item.cost_price) +
          parseFloat(item.markup) +
          parseFloat(scope.row.withholding_fee) +
          parseFloat(item.purchasePrice)
        ).toFixed(4);
      }
    },
    //删除
    remove(scope) {
      let id = scope.row.erp_goods_code;
      this.tableData.forEach((res, index) => {
        if (res.erp_goods_code == id) {
          this.tableData.splice(index, 1);
        }
      });
      this.priceNum = 0;
    },
    // 添加商品
    addGoods() {
      if (this.tableData.length == 1) {
        this.$message({
          type: "info",
          message: "商品已添加"
        });
      } else {
        if (this.commodity) {
          let every = this.tableData.filter(res => {
            return (
              res.Code == this.commodity || res.erp_goods_code == this.commodity
            );
          });
          if (every.length == 0) {
            let arr = this.goodsList.filter(res => {
              return res.Code == this.commodity;
            });

            this.pushData(arr[0]);
          } else {
            this.$message({
              type: "info",
              message: "该商品已添加"
            });
          }
        } else {
          this.$message({
            type: "info",
            message: "请选择商品"
          });
        }
      }
    },

    // 获取商品方法
    insideRemoteMethod(query) {
      if (query !== "") {
        this.getSupplierList(query);
      } else {
        this.inside.options = [];
      }
    },
    //获取商品数据
    getSupplierList(query) {
      this.search.goods_code = query;
      this.getData();
    },
    // 嵌套搜索机构数据
    organizationMethod(query) {
      if (query !== "") {
        this.tableData[0].orgCode = [];
        this.organizationList(query);
      } else {
        this.insides.option = [];
      }
    },
    //获取机构数据
    organizationList(query) {
      this.search.org_code = query;
      this.getDatas();
    },
    //添加规格
    addSpec(scope) {
      this.specId = scope.row.BarCode;
      this.spaceDialog = true;
    },
    cancelSpec() {
      this.spaceDialog = false;
      this.spec = 0;
      this.specId = "";
    },
    //修改数量
    orderNum(scope, prop) {
      if (scope.row.rk_specs) {
        let rk_specs = parseFloat(scope.row.rk_specs);
        prop.box_qty = (prop.order_num / rk_specs).toFixed(4);
      }
    },
    //修改件数
    qtyup(scope, prop) {
      if (scope.row.rk_specs) {
        let rk_specs = parseFloat(scope.row.rk_specs);
        prop.order_num = (prop.box_qty * rk_specs).toFixed(4);
      }
    },
    //行采切换
    hangCai() {
      console.log(this.is_hangCai, 99999);
      this.is_assign = false;
      // this.supplier_code = "";
      // this.buyer_code = "";
      // this.buyer_name = "";
      // this.buyer_id = "";
      if (this.is_hangCai) {
        // this.getSupplier();
        this.getPurchase();
      }

      // this.supplierCenter = {
      //   innerVisible: false,
      //   option: [],
      //   value: "",
      //   list: [],
      //   loading: false,
      // };
      // this.purchase = {
      //   innerVisible: false,
      //   option: [],
      //   value: "",
      //   list: [],
      //   loading: false,
      // };
      this.tableData = [];
    },
    //预提切换
    assign() {
      console.log(this.is_assign);
      this.tableData = [];
    },
    //规格确定
    specNum() {
      this.tableData.forEach((res, index) => {
        if (res.BarCode == this.specId) {
          res.SonCountsList.push({
            value: this.spec,
            label: this.spec
          });
          this.$set(this.tableData, index, res);
          this.cancelSpec();
        }
      });
    },
    sonCount(scope) {
      if (scope.row.order_num) {
        scope.row.box_qty = (
          parseFloat(scope.row.order_num) / parseFloat(scope.row.rk_spec)
        ).toFixed(4);
        this.$set(this.tableData, scope.$index, scope.row);
      }
    },
    pushData(scope) {
      let data = {
        dccode: this.selectList,
        erp_goods_code: scope.Code,
        supplier_code: this.search.supplier_code
      };
      getUnAvgBatch(data)
        .then(res => {
          let unAvgBatchList = [];
          res.data.paginate.forEach(item => {
            let codeList = item.code.split(",");
            let codeStr =
              codeList.length > 1 ? codeList[0] + "..." : codeList[0];
            unAvgBatchList.push({
              value: `${item.id}`,
              label: `${item.goods_name}【${codeStr}】【生产日期:${
                item.produce_date ? item.produce_date : "无"
              }】(${item.stock_num}/${item.rk_spec})`,
              price: `${item.price}`,
              withholding_fee: `${item.withholding_fee}`,
              markup_price: `${item.markup_price}`,
              rk_spec: `${item.rk_spec}`,
              code: `${item.code}`,
              produce_date: `${item.produce_date}`,
              shelf_life: `${item.shelf_life}`,
              due_date: `${item.due_date}`
            });
          });
          return unAvgBatchList;
        })
        .then(unAvgBatchList => {
          let cost_price = 0;
          let withholding_fees = 0;
          let sonObj = [
            {
              value: 1,
              label: 1
            }
          ];
          let purchasePriceCenter = (
            Math.round(
              (parseFloat(scope.avg_price) +
                parseFloat(scope.withholding_fee)) *
                parseFloat(scope.BuyerProfitRate) *
                10000
            ) / 10000
          ).toFixed(4);
          let purchasePrice = scope.BuyerProfitMode
            ? scope.BuyerProfitRate
            : purchasePriceCenter;
          let stockRemoval = (
            parseFloat(scope.avg_price) +
            parseFloat(scope.withholding_fee) +
            parseFloat(purchasePrice)
          ).toFixed(4);
          let stockRemovals = (
            parseFloat(cost_price) + parseFloat(withholding_fees)
          ).toFixed(4);
          console.log(scope, 999999);
          let rdcBool = null;
          if (this.rdc.length > 0) {
            rdcBool = this.rdc.some(res => {
              return res == this.distributionCenter.rdc_code;
            });
          }
          this.tableData.push({
            erp_goods_code: scope.Code,
            erp_goods_id: scope.ProductId || 0,
            goods_name: scope.Name,
            bar_code: scope.BarCode,
            goods_unit: scope.Unit,
            goods_spec: scope.Spec || "",
            category_code: scope.ClassCode,
            category_name: scope.ClassName,
            is_zc: 1,
            avg_price: scope.avg_price,
            cost_price: cost_price,
            stockRemoval: stockRemoval,
            stockRemovals: stockRemovals,
            purchasePrice: purchasePrice,
            BuyerProfitMode: scope.BuyerProfitMode,
            BuyerProfitRate: scope.BuyerProfitRate,
            totalPrices: 0,
            totalPricess: 0,
            total_price: 0,
            order_num: 0,
            box_qty: 0,
            withholding_fee: this.is_hangCai ? 0 : scope.withholding_fee,
            withholding_fees: withholding_fees,
            package_box_id: rdcBool ? this.boxList[1].id : this.boxList[0].id,
            package_box_specInp: rdcBool ? scope.package_box_spec : 1,
            package_box_spec: scope.package_box_spec,
            rdcType: rdcBool ? true : false,
            m_type: scope.m_type,
            markup: scope.markup_price,
            markups: scope.markup,
            batch_id: "",
            unAvgBatchList,
            rk_spec: 1,
            SonCountsList: sonObj,
            produce_date: "",
            shelf_life: scope.ShelfLife,
            due_date: "",
            orgList: [],
            orgCode: "",
            reference_price: scope.reference_price,
            fixed_priceType:
              parseFloat(scope.fixed_price) && this.is_assign ? true : false,
            fixed_price: parseFloat(scope.fixed_price),
            rk_specs: !this.is_assign && !this.is_hangCai ? scope.rk_spec : 0,
            notes: ""
          });
          console.log(this.tableData, 123456);
        });
    },
    //修改门店到货价
    orderFixedPprice(scope) {
      if (scope.row.fixed_price) {
        scope.row.fixed_priceType = true;
        scope.row.orgList.forEach(res => {
          res.stockRemoval = scope.row.fixed_price;
          if (!scope.row.BuyerProfitMode) {
            let num = (
              parseFloat(scope.row.fixed_price) /
              (1 + parseFloat(scope.row.BuyerProfitRate))
            ).toFixed(4);
            res.purchasePrice = (
              parseFloat(scope.row.fixed_price) - num
            ).toFixed(4);
          } else {
            res.purchasePrice = parseFloat(scope.row.BuyerProfitRate);
          }
          res.markup = (
            parseFloat(scope.row.fixed_price) -
            parseFloat(scope.row.cost_price) -
            parseFloat(scope.row.withholding_fees) -
            parseFloat(res.purchasePrice)
          ).toFixed(4);
          // res.purchasePrice = 0;
          // res.markup = (
          //   parseFloat(scope.row.fixed_price) -
          //   parseFloat(scope.row.withholding_fees) -
          //   parseFloat(scope.row.cost_price)
          // ).toFixed(4);
        });
      } else {
        scope.row.fixed_priceType = false;
        scope.row.fixed_price = 0;
        scope.row.orgList.forEach(res => {
          res.cost_price = scope.row.cost_price;
          res.markup = 0;
          let purchasePriceCenter = (
            Math.round(
              (parseFloat(res.cost_price) +
                parseFloat(res.markup) +
                parseFloat(scope.row.withholding_fees)) *
                parseFloat(scope.row.BuyerProfitRate) *
                10000
            ) / 10000
          ).toFixed(4);
          res.purchasePrice = scope.row.BuyerProfitMode
            ? scope.row.BuyerProfitRate
            : purchasePriceCenter;
          console.log(
            res.cost_price,
            res.markup,
            scope.row.withholding_fees,
            res.purchasePrice
          );
          res.stockRemoval = (
            parseFloat(res.cost_price) +
            parseFloat(res.markup) +
            parseFloat(scope.row.withholding_fees) +
            parseFloat(res.purchasePrice)
          ).toFixed(4);
        });
      }
    },
    //选择批次
    onBatch(scope) {
      scope.row.cost_price = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[0];
      scope.row.withholding_fees = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[1];
      let markup_price = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[2];
      scope.row.rk_specs = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[3];
      scope.row.produce_date = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[4];
      scope.row.shelf_life = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[5];
      scope.row.due_date = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[6];
      if (scope.row.rk_specs) {
        let rk_specs = parseFloat(scope.row.rk_specs);
        scope.row.orgList.forEach(prop => {
          prop.box_qty = (prop.order_num / rk_specs).toFixed(4);
        });
      }
      if (scope.row.fixed_price) {
        scope.row.fixed_priceType = true;
        scope.row.orgList.forEach(res => {
          res.stockRemoval = scope.row.fixed_price;
          res.cost_price = scope.row.cost_price;
          // res.purchasePrice = 0;
          // res.markup = (
          //   parseFloat(scope.row.fixed_price) -
          //   parseFloat(scope.row.withholding_fees) -
          //   parseFloat(scope.row.cost_price)
          // ).toFixed(4);
          if (!scope.row.BuyerProfitMode) {
            let num = (
              parseFloat(scope.row.fixed_price) /
              (1 + parseFloat(scope.row.BuyerProfitRate))
            ).toFixed(4);
            res.purchasePrice = (
              parseFloat(scope.row.fixed_price) - num
            ).toFixed(4);
          } else {
            res.purchasePrice = parseFloat(scope.row.BuyerProfitRate);
          }
          res.markup = (
            parseFloat(scope.row.fixed_price) -
            parseFloat(scope.row.cost_price) -
            parseFloat(scope.row.withholding_fees) -
            parseFloat(res.purchasePrice)
          ).toFixed(4);
        });
      } else {
        scope.row.fixed_priceType = false;
        scope.row.fixed_price = 0;
        scope.row.markup = markup_price;
        scope.row.orgList.forEach(res => {
          res.cost_price = scope.row.cost_price;
          res.markup = markup_price;
          let purchasePriceCenter = (
            Math.round(
              (parseFloat(res.cost_price) +
                parseFloat(scope.row.withholding_fees) +
                parseFloat(res.markup)) *
                parseFloat(scope.row.BuyerProfitRate) *
                10000
            ) / 10000
          ).toFixed(4);
          res.purchasePrice = scope.row.BuyerProfitMode
            ? scope.row.BuyerProfitRate
            : purchasePriceCenter;
          res.stockRemoval = (
            parseFloat(res.cost_price) +
            parseFloat(res.markup) +
            parseFloat(scope.row.withholding_fees) +
            parseFloat(res.purchasePrice)
          ).toFixed(4);
        });
      }
    },
    batchPrice(id, unAvgBatchList) {
      let price = 0;
      let withholding_fee = 0;
      let markup_price = 0;
      let rk_spec = 0;
      let produce_date = "";
      let shelf_life = "";
      let due_date = "";
      unAvgBatchList.forEach(res => {
        if (res.value == id) {
          price = res.price;
          withholding_fee = res.withholding_fee;
          markup_price = res.markup_price;
          rk_spec = res.rk_spec;
          produce_date = res.produce_date;
          shelf_life = res.shelf_life;
          due_date = res.due_date;
        }
      });
      return [
        price,
        withholding_fee,
        markup_price,
        rk_spec,
        produce_date,
        shelf_life,
        due_date
      ];
    },
    //添加机构
    addOrganization(scope, propData, insidesList) {
      console.log(scope, "11", propData, "22", insidesList, "333");
      if (propData) {
        scope.row.orgCode = [propData.org_info.erp_org_code];
        this.insides.option = insidesList.map(item => {
          return {
            value: `${item.erp_org_code}`,
            label: `${item.name}`,
            org_area_code: `${item.org_area_code}`
          };
        });
      }
      console.log(scope.row.orgCode, 9999);
      if (scope.row.orgCode.length > 0) {
        let bool = true;

        scope.row.orgList.forEach(e => {
          scope.row.orgCode.forEach(item => {
            if (parseInt(item) == parseInt(e.orgCode)) {
              bool = false;
            }
          });
        });
        if (bool) {
          scope.row.orgCode.forEach(prop => {
            if (scope.row.orgList.length > 0) {
              let arr = scope.row.orgList.filter(res => {
                return res.orgCode == prop;
              });
              if (arr.length > 0) {
                this.$message({
                  type: "info",
                  message: "该机构已添加"
                });
              } else {
                let orgArr = this.insides.option.filter(res => {
                  return res.value == prop;
                });
                let referencePrice = scope.row.reference_price[2];
                let reference_price = referencePrice
                  ? referencePrice[orgArr[0].org_area_code]
                    ? referencePrice[orgArr[0].org_area_code].reference_price
                    : 0
                  : 0;
                let cost_price = this.is_hangCai
                  ? reference_price
                  : !this.is_assign
                  ? scope.row.avg_price
                  : scope.row.cost_price;
                let withholding_fee = !this.is_assign
                  ? scope.row.withholding_fee
                  : scope.row.withholding_fees;
                if (scope.row.fixed_price && this.is_assign) {
                  scope.row.fixed_priceType = true;
                  if (scope.row.batch_id) {
                    let purchasePrice = 0;
                    if (!scope.row.BuyerProfitMode) {
                      let num = (
                        parseFloat(scope.row.fixed_price) /
                        (1 + parseFloat(scope.row.BuyerProfitRate))
                      ).toFixed(4);
                      purchasePrice = (
                        parseFloat(scope.row.fixed_price) - num
                      ).toFixed(4);
                    } else {
                      purchasePrice = parseFloat(scope.row.BuyerProfitRate);
                    }
                    let markup = (
                      parseFloat(scope.row.fixed_price) -
                      parseFloat(scope.row.cost_price) -
                      parseFloat(scope.row.withholding_fees) -
                      parseFloat(purchasePrice)
                    ).toFixed(4);
                    scope.row.orgList.push({
                      orgCode: prop,
                      orgName: orgArr[0].label,
                      order_num: propData ? propData.num : 0,
                      box_qty: scope.row.rk_specs
                        ? propData
                          ? (
                              parseFloat(propData.num) /
                              parseFloat(scope.row.rk_specs)
                            ).toFixed(4)
                          : 0
                        : 0,
                      markup: markup,
                      cost_price: cost_price,
                      purchasePrice: purchasePrice,
                      stockRemoval: scope.row.fixed_price,
                      reference_price: reference_price
                    });
                  } else {
                    scope.row.orgList.push({
                      orgCode: prop,
                      orgName: orgArr[0].label,
                      order_num: propData ? propData.num : 0,
                      box_qty: scope.row.rk_specs
                        ? propData
                          ? (
                              parseFloat(propData.num) /
                              parseFloat(scope.row.rk_specs)
                            ).toFixed(4)
                          : 0
                        : 0,
                      markup: 0,
                      cost_price: cost_price,
                      purchasePrice: 0,
                      stockRemoval: scope.row.fixed_price,
                      reference_price: reference_price
                    });
                  }
                } else {
                  let markup = 0;
                  if (this.is_hangCai) {
                    console.log(scope.row, 11111111);
                    if (scope.row.m_type == 1) {
                      markup = scope.row.markups;
                    } else {
                      markup = (cost_price * (scope.row.markups / 100)).toFixed(
                        4
                      );
                    }
                  } else if (!this.is_assign) {
                    markup = scope.row.markup;
                  } else if (this.is_assign && scope.row.batch_id) {
                    markup = propData
                      ? propData.jj
                        ? propData.jj
                        : scope.row.markup
                      : scope.row.markup;
                  } else {
                    markup = propData ? propData.jj : 0;
                  }
                  let purchasePriceCenter = (
                    Math.round(
                      (parseFloat(cost_price) +
                        parseFloat(markup) +
                        parseFloat(withholding_fee)) *
                        parseFloat(scope.row.BuyerProfitRate) *
                        10000
                    ) / 10000
                  ).toFixed(4);
                  let purchasePrice = scope.row.BuyerProfitMode
                    ? scope.row.BuyerProfitRate
                    : purchasePriceCenter;
                  scope.row.fixed_priceType = false;
                  scope.row.fixed_price = 0;
                  scope.row.orgList.push({
                    orgCode: prop,
                    orgName: orgArr[0].label,
                    order_num: propData ? propData.num : 0,
                    box_qty: scope.row.rk_specs
                      ? propData
                        ? (
                            parseFloat(propData.num) /
                            parseFloat(scope.row.rk_specs)
                          ).toFixed(4)
                        : 0
                      : 0,
                    markup: markup,
                    markups: scope.row.markups,
                    cost_price: cost_price,
                    purchasePrice: purchasePrice,
                    reference_price: reference_price,
                    stockRemoval: (
                      parseFloat(cost_price) +
                      parseFloat(markup) +
                      parseFloat(withholding_fee) +
                      parseFloat(purchasePrice)
                    ).toFixed(4)
                  });
                }
              }
            } else {
              console.log(6789);
              let orgArr = this.insides.option.filter(res => {
                return res.value == prop;
              });
              let referencePrice = scope.row.reference_price[2];
              let reference_price = referencePrice
                ? referencePrice[orgArr[0].org_area_code]
                  ? referencePrice[orgArr[0].org_area_code].reference_price
                  : 0
                : 0;

              let cost_price = this.is_hangCai
                ? reference_price
                : !this.is_assign
                ? scope.row.avg_price
                : scope.row.cost_price;
              let withholding_fee = !this.is_assign
                ? scope.row.withholding_fee
                : scope.row.withholding_fees;

              if (scope.row.fixed_price && this.is_assign) {
                scope.row.fixed_priceType = true;
                if (scope.row.batch_id) {
                  let purchasePrice = 0;
                  if (!scope.row.BuyerProfitMode) {
                    let num = (
                      parseFloat(scope.row.fixed_price) /
                      (1 + parseFloat(scope.row.BuyerProfitRate))
                    ).toFixed(4);
                    purchasePrice = (
                      parseFloat(scope.row.fixed_price) - num
                    ).toFixed(4);
                  } else {
                    purchasePrice = parseFloat(scope.row.BuyerProfitRate);
                  }
                  let markup = (
                    parseFloat(scope.row.fixed_price) -
                    parseFloat(scope.row.cost_price) -
                    parseFloat(scope.row.withholding_fees) -
                    parseFloat(purchasePrice)
                  ).toFixed(4);
                  scope.row.orgList.push({
                    orgCode: prop,
                    orgName: orgArr[0].label,
                    order_num: propData ? propData.num : 0,
                    box_qty: scope.row.rk_specs
                      ? propData
                        ? (
                            parseFloat(propData.num) /
                            parseFloat(scope.row.rk_specs)
                          ).toFixed(4)
                        : 0
                      : 0,
                    markup: markup,
                    cost_price: cost_price,
                    purchasePrice: purchasePrice,
                    stockRemoval: scope.row.fixed_price
                  });
                } else {
                  scope.row.orgList.push({
                    orgCode: prop,
                    orgName: orgArr[0].label,
                    order_num: propData ? propData.num : 0,
                    box_qty: scope.row.rk_specs
                      ? propData
                        ? (
                            parseFloat(propData.num) /
                            parseFloat(scope.row.rk_specs)
                          ).toFixed(4)
                        : 0
                      : 0,
                    markup: 0,
                    cost_price: cost_price,
                    purchasePrice: 0,
                    stockRemoval: scope.row.fixed_price,
                    reference_price: reference_price
                  });
                }
              } else {
                console.log(1);
                let markup = 0;
                if (this.is_hangCai) {
                  console.log(scope.row, 2);
                  if (scope.row.m_type == 1) {
                    markup = scope.row.markups;
                  } else {
                    markup = (cost_price * (scope.row.markups / 100)).toFixed(
                      4
                    );
                  }
                } else if (!this.is_assign) {
                  console.log(3);
                  markup = scope.row.markup;
                } else if (this.is_assign && scope.row.batch_id) {
                  console.log(scope.row, 4);
                  markup = propData
                    ? propData.jj
                      ? propData.jj
                      : scope.row.markup
                    : scope.row.markup;
                  // console.log(propData, 10);
                  // console.log(propData.jj, 11);
                  console.log(markup, 12);
                } else {
                  console.log(5);
                  markup = propData ? propData.jj : 0;
                }
                scope.row.fixed_priceType = false;
                scope.row.fixed_price = 0;
                let purchasePriceCenter = (
                  Math.round(
                    (parseFloat(cost_price) +
                      parseFloat(markup) +
                      parseFloat(withholding_fee)) *
                      parseFloat(scope.row.BuyerProfitRate) *
                      10000
                  ) / 10000
                ).toFixed(4);
                let purchasePrice = scope.row.BuyerProfitMode
                  ? scope.row.BuyerProfitRate
                  : purchasePriceCenter;
                scope.row.orgList.push({
                  orgCode: prop,
                  orgName: orgArr[0].label,
                  order_num: propData ? propData.num : 0,
                  box_qty: scope.row.rk_specs
                    ? propData
                      ? (
                          parseFloat(propData.num) /
                          parseFloat(scope.row.rk_specs)
                        ).toFixed(4)
                      : 0
                    : 0,
                  markup: markup,
                  markups: scope.row.markups,
                  cost_price: cost_price,
                  purchasePrice: purchasePrice,
                  reference_price: reference_price,
                  stockRemoval: (
                    parseFloat(cost_price) +
                    parseFloat(markup) +
                    parseFloat(withholding_fee) +
                    parseFloat(purchasePrice)
                  ).toFixed(4)
                });
              }
            }
            scope.row.orgCode = [];
          });
          // this.isUpdates();
          this.isUpdate++;
        } else {
          this.$message({
            type: "info",
            message: "添加的机构中包含已选中的机构，请重新勾选"
          });
        }
      } else {
        this.$message({
          type: "info",
          message: "请选择机构"
        });
      }
    },
    isUpdates() {
      this.$nextTick(() => {
        this.$refs.isUpdate.doLayout();
      });
    },
    //删除机构
    removeOrg(scope, index) {
      scope.row.orgList.splice(index, 1);
    },
    // 导出模版
    templateDownload() {
      this.$confirm("是否下载模版?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");

          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Norder/downLoadOrderOrgGoodsInfoTem?token=" +
            getToken();
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下载模版"
          });
        });
    },
    //导入模版
    handleAvatarSuccess(param) {
      let loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)"
      });
      let data = new window.FormData();
      data.append("order_org_goods_info", param.file);
      data.append("token", getToken());
      this.$axios.post("/smkapi/Norder/importOrderTem", data).then(res => {
        loading.close();
        if (res.data.code == 0) {
          this.tableData[0].orgList = [];
          res.data.data.forEach(prop => {
            let modeData = {
              row: this.tableData[0]
            };
            this.addOrganization(modeData, prop, res.data.orglist);
          });
        } else {
          this.$message({
            type: "info",
            message: res.data.message || res.data.msg
          });
        }
      });
    },
    //确定
    confirm() {
      if (this.selectList == "") {
        this.$message({
          type: "info",
          message: "请选择配送中心"
        });
      } else if (this.tableData.length == "") {
        this.$message({
          type: "info",
          message: "请添加商品"
        });
      } else {
        this.$confirm(
          "请核对订单发货信息，点击确认发货后，将直接发货无法更改?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            let arr = [];
            this.tableData.forEach(res => {
              let orgArr = [];
              res.orgList.forEach((item, index) => {
                let orgCode = item.orgCode;
                orgArr[index] = {
                  [orgCode]: {
                    order_num: item.order_num,
                    box_qty: item.box_qty,
                    markup: item.markup,
                    cost_price: item.cost_price
                  }
                };
              });
              arr.push({
                erp_goods_code: res.erp_goods_code,
                erp_goods_id: res.erp_goods_id,
                goods_name: res.goods_name,
                bar_code: res.bar_code,
                goods_unit: res.goods_unit,
                goods_spec: res.goods_spec || "",
                category_code: res.category_code,
                category_name: res.category_name,
                is_zc: 1,
                withholding_fee: !this.is_assign
                  ? res.withholding_fee
                  : res.withholding_fees,
                produce_date: res.produce_date,
                shelf_life: res.shelf_life,
                due_date: res.due_date,
                rk_spec: this.is_hangCai ? res.rk_spec : res.rk_specs,
                batch_id: res.batch_id,
                orglist: orgArr,
                fixed_price: res.fixed_price,
                buyerprofitmode: res.BuyerProfitMode,
                buyerprofitrate: res.BuyerProfitRate,
                package_box_id: res.package_box_id,
                package_box_spec: res.package_box_specInp,
                notes: res.notes
              });
            });
            let data = {
              hangcai_flag: this.is_hangCai ? 1 : 0,
              is_assign: this.is_assign ? 1 : 0,
              supplier_code: this.search.supplier_code,
              buyer_code: this.is_hangCai ? this.search.buyer_code : "",
              buyer_name: this.is_hangCai ? this.search.buyer_name : "",
              buyer_id: this.is_hangCai ? this.search.buyer_id : "",
              goods: JSON.stringify(arr),
              dccode: this.selectList
            };
            createOrderByMultiStore(data).then(res => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: res.msg || res.message
                });
                this.$emit("upData");
                this.cancel();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      }
    },
    //只生成订单
    addOrder() {
      if (this.selectList == "") {
        this.$message({
          type: "info",
          message: "请选择配送中心"
        });
      } else if (this.tableData.length == "") {
        this.$message({
          type: "info",
          message: "请添加商品"
        });
      } else {
        this.$confirm("请核对订单发货信息，此按钮为只生成订单?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let arr = [];
            this.tableData.forEach(res => {
              let orgArr = [];
              res.orgList.forEach((item, index) => {
                let orgCode = item.orgCode;
                orgArr[index] = {
                  [orgCode]: {
                    order_num: item.order_num,
                    box_qty: item.box_qty,
                    markup: item.markup,
                    cost_price: item.cost_price
                  }
                };
              });
              arr.push({
                erp_goods_code: res.erp_goods_code,
                erp_goods_id: res.erp_goods_id,
                goods_name: res.goods_name,
                bar_code: res.bar_code,
                goods_unit: res.goods_unit,
                goods_spec: res.goods_spec || "",
                category_code: res.category_code,
                category_name: res.category_name,
                is_zc: 1,
                withholding_fee: !this.is_assign
                  ? res.withholding_fee
                  : res.withholding_fees,
                produce_date: res.produce_date,
                shelf_life: res.shelf_life,
                due_date: res.due_date,
                rk_spec: this.is_hangCai ? res.rk_spec : res.rk_specs,
                batch_id: res.batch_id,
                orglist: orgArr,
                fixed_price: res.fixed_price,
                buyerprofitmode: res.BuyerProfitMode,
                buyerprofitrate: res.BuyerProfitRate,
                package_box_id: res.package_box_id,
                package_box_spec: res.package_box_specInp
              });
            });
            let data = {
              hangcai_flag: this.is_hangCai ? 1 : 0,
              is_assign: this.is_assign ? 1 : 0,
              supplier_code: this.search.supplier_code,
              buyer_code: this.is_hangCai ? this.search.buyer_code : "",
              buyer_name: this.is_hangCai ? this.search.buyer_name : "",
              buyer_id: this.is_hangCai ? this.search.buyer_id : "",
              goods: JSON.stringify(arr),
              dccode: this.selectList
            };
            createOrderByMultiStoreNoSend(data).then(res => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: res.msg || res.message
                });
                this.$emit("upData");
                this.cancel();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      }
    }
  }
};
</script>
<style scoped>
.pagination {
  text-align: right;
  padding: 10px;
}

.top {
  margin-bottom: 5px;
}

.orgTop {
  position: relative;
}

.orgTop .remBtn {
  position: absolute;
  right: 0;
  top: -5px;
}
</style>
